package com.genghis.rapidcrm.web.view.support;

import edu.jetwinner.common.EasyStringUtil;

public final class RapidLoader {

    private RapidLoader() {
        // reserved.
    }

    public static final String DIRECTORY_SEPARATOR = "/";

    private static final String DEFAULT_TEMPLATE_FILE_EXTENSION = "html";

    public static String resolveNameToPath(String qualifiedName) {
        return resolveNameToPath(qualifiedName, DEFAULT_TEMPLATE_FILE_EXTENSION);
    }
    public static String resolveNameToPath(String qualifiedName, String fileExtension) {
        String[] allowedExtensions = new String[]{DEFAULT_TEMPLATE_FILE_EXTENSION, "js", "css", "less"};

        String file = "";
        if (!EasyStringUtil.inArray(fileExtension, allowedExtensions)) {
            return "";
        }
        // :TODO will test later.
        if (qualifiedName.indexOf("~~") == 0) {
            file = qualifiedName.replaceAll("~~", "");
        } else if (qualifiedName.indexOf("~") == 0) {
            file = qualifiedName.replaceAll("~", "");
        } else {
            file = qualifiedName.replaceAll("\\.", DIRECTORY_SEPARATOR) + "." + fileExtension;
        }
        if (!file.startsWith(DIRECTORY_SEPARATOR)) {
            file = DIRECTORY_SEPARATOR + file;
        }
        return file;
    }
}
